﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Tags.Interfaces;
using WordSegments;

using ZCrawler;


namespace Tags
{
    public class TagManager
    {
        public static List<string> GetTag(string tag, List<string> words)
        {
            GetTagDescFromSource(tag, (result) =>
            {
                words = OPSegmentUtils.sharedInstance.strFenCiIndexer(tag).Split(' ').ToList();
            });
            return words;
        }
        private static void GetTagDescFromSource(string tag,Action<string> onResultGet)
        {
            ZTask task = new ZTask()
            {
                Url = "http://baike.baidu.com/search/word?word=" + tag
            };
            task.onStepDataOut = (t, result) =>
            {
                if(onResultGet != null)
                {
                    onResultGet(result.DATA["descr"].ToString());
                }
            };
            List<ZBasicRule> rules = new List<ZBasicRule>();
            ZRuleXPath rule_desc = new ZRuleXPath(ZXPathType.InnerText, "descr");
            rule_desc.AddXPath("//*[@id=\"card-container\"]/div/div/div");
            rules.Add(rule_desc);
            ZAnalysis analysis = new ZAnalysis(rules);
            task.analysis = analysis;
            task.Launch();
        }
    }
}
